package com.ruiysoft.crm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruiysoft.crm.model.entity.BizClient;
import com.ruiysoft.crm.model.vo.BizClientVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.Map;

/**
 * @author fanyl
 */
public interface BizClientMapper extends BaseMapper<BizClient> {

    List<BizClient> queryClientAndCarInfo(IPage page, @Param("params") Map<String, Object> params);

    @Select("select client.*,dict.label from biz_client client LEFT JOIN sys_dict dict on client.client_type=dict.value AND dict.`type`='clientType'  where client.id=#{id}")
    BizClient getClientById(int id);

    @Update("update biz_client set user_id=null,user_name=null,success=null,allocationDate=null where id in (${clientIds })")
    boolean revokeAllotUser(@Param("clientIds") String clientIds);

    @Update("update biz_client set user_id=#{userId },user_name=#{realName },allocationDate=now() where id in (${clientIds })")
    int allocationByBatchid(@Param("batchid") String batchid, @Param("userId") Integer userId, @Param("realName") String realName, @Param("clientIds") String clientIds);


    @Select("select car_no from biz_client")
    List<String> getCarNos();

    @Select("select enterprise_id from biz_client")
    List<String> getEnterpriseCode();


    List<BizClientVo> queryClients(@Param("params") Map<String, Object> params);

    @Select("SELECT\n" +
            "\tdistinct biz_call_log.createId,\n" +
            "\tsys_user.real_name as realName,\n" +
            "\tbiz_client.* \n" +
            "FROM\n" +
            "\tbiz_client\n" +
            "\tLEFT JOIN biz_call_log ON biz_client.id = biz_call_log.clientId \n" +
            "\tleft join sys_user on biz_call_log.createId = sys_user.user_id\n" +
            "WHERE\n" +
            "\tbiz_client.user_id = #{userId} and biz_call_log.createId is not null")
    List<BizClient> clientInfoLeftJoinCallLog(@Param("userId") Integer arg0);

    int updateByMySelf(@Param("bizClient") BizClient arg0);

    @Select("<script> SELECT COUNT(CLIENT.id) seating,IFNULL(dic.`label`,'销售数据') label FROM biz_client CLIENT LEFT JOIN sys_dict dic ON client.`success`=dic.`value`  AND dic.`type`='clientGrouping' \n" +
            "WHERE 1=1 <if test='userId !=null' > and client.`user_id`=#{userId} </if> GROUP BY IFNULL(dic.`label`,'销售数据') </script>")
    List<BizClient> getSuccessCountByUserId(@Param("userId") String userId);
}
